/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package services.ie.com.stamply.image


/**
 *
 * @author Fabiano
 */

class PhotoService {
    
    def photo
    def burningImageService
    

    /*
    void createImage(boolean business, String username, String rootDir){
        def dir = new File(rootDir, "/images/" + (business ? "business" : "customers") + "/${username}/profile")
        if (dir.isDirectory()){
            if (dir.exists()){
                //copyDirectory(dir, new File (rootDir, "/images/" + (business ? "business" : "customers") + "/${username}/profile_" + Date().toString()))
                renameDir(dir, new File (rootDir, "/images/" + (business ? "business" : "customers") + "/${username}/profile" ))
            }
        } else dir.mkdirs()
        saveImagem(dir)
        
    }
    */
    
    void createImage(String rootDir){
        def dir = new File(rootDir)
        if (dir.isDirectory()){
            if (dir.exists()){
                //copyDirectory(dir, new File (rootDir, "/images/" + (business ? "business" : "customers") + "/${username}/profile_" + Date().toString()))
                renameDir(dir, new File (rootDir, "_old" ))
            }
        } else dir.mkdirs()
        saveImagem(dir)        
    }
    
    void renameDir(File oldDir, File newDir){
      boolean success = oldDir.renameTo(newDir);
      if (!success) {
      // File was not successfully renamed
      }         
    }
    /*
    void coyDirectory(File dirOrigem, File dirDestination)throws IOException {
        if (dirOrigem.isDirectory()) {
           if (!dirDestination.exists()) {
                dirDestination.mkdir();
           }

           String[] children = dirOrigem.list();
           for (int i=0; i<children.length; i++) {
               copyDirectory(new File(dirOrigem, children[i]),
                             new File(dirDestination, children[i]));
           }
        } else {
        // This method is implemented in Copying a File
            copyFile(dirOrigem, dirDestination);
        }
    }   
    
    void copyFile (File src, File dst) throws IOException {
       InputStream in = new FileInputStream(src);
       OutputStream out = new FileOutputStream(dst);

    // Transfer bytes from in to out
       byte[] buf = new byte[1024];
       int len;
       while ((len = in.read(buf)) > 0) {
           out.write(buf, 0, len);
       }
       in.close();
       out.close();
   }
    
    void copyFiles(){
        
    }
    */
    void saveImagem(File dir){
           burningImageService.doWith(photo, dir.getPath())
                   .execute {
                       it.scaleApproximate(800, 600)
                    }
                   .execute ('tiny', {
                       it.scaleAccurate(25, 25)
                    })   
                   .execute ('topbar', {
                       it.scaleAccurate(16, 16)
                    }) 
                   .execute ('small', {
                       it.scaleAccurate(40, 40)
                    }) 
                   .execute ('medium', {
                       it.scaleAccurate(100, 100)
                    }) 
                
                   .execute ('master', {
                       it.scaleAccurate(378, 331)
                    }) 
                
                   .execute ('large', {
                       it.scaleAccurate(200, 200)
                    })         
       }
    }
	

   
